(function() {
  var S;

  S = {};

  window.S = S;

}).call(this);

(function() {
  var AjaxOption, api, apiRoot, failHandler;

  api = {};

  S.api = api;

  apiRoot = "/";

  AjaxOption = function(type, data, settings) {
    var k, results, vl;
    this.type = type;
    if (type === "POST" || type === "PUT" || type === "DELETE") {
      this.data = data && JSON.stringify(data);
      this.contentType = "application/json";
    } else {
      this.data = data;
    }
    this.cache = false;
    if (settings != null) {
      results = [];
      for (k in settings) {
        vl = settings[k];
        results.push(this[k] = v);
      }
      return results;
    }
  };

  failHandler = function(jqxhr) {
    if (jqxhr.status === 400) {
      return true;
    }
  };

  api.get = function(relativeUrl, data, settings) {
    return $.ajax(apiRoot + relativeUrl, new AjaxOption("GET", data, settings)).fail(failHandler);
  };

  api.getAbsolute = function(absoluteUrl, data, settings) {
    return $.ajax(absoluteUrl, new AjaxOption("GET", data, settings)).fail(failHandler);
  };

  api.post = function(relativeUrl, data, settings) {
    return $.ajax(apiRoot + relativeUrl, new AjaxOption("POST", data, settings)).fail(failHandler);
  };

  api.put = function(relativeUrl, data) {
    return $.ajax(apiRoot + relativeUrl, new AjaxOption("PUT", data)).fail(failHandler);
  };

  api.remove = function(relativeUrl, data) {
    return $.ajax(apiRoot + relativeUrl, new AjaxOption("DELETE", data)).fail(failHandler);
  };

}).call(this);

(function() {
  S.alertAjaxError = function(xhr) {
    return alert(S.parseXhrErrorMessage(xhr));
  };

  S.parseXhrErrorMessage = function(xhr) {
    try {
      return xhr.responseText && JSON.parse(xhr.responseText).message || xhr.responseText;
    } catch (error) {
      return xhr.responseText;
    }
  };

  S.alert = function(message) {
    return alert(message);
  };

  S.toast = function(message) {
    return alert(message);
  };

}).call(this);

(function() {
  var $toastContainer, Toast, showDialog;

  $toastContainer = $('.toast-container');

  Toast = (function() {
    function Toast(message1) {
      this.message = message1;
      console.log(this.message);
      this.init(this.message);
    }

    Toast.prototype.init = function(message, type) {
      var $toast, distance, speed;
      distance = '120px';
      speed = 300;
      $toast = $("<div class='toast' style='opacity: 0;top:" + distance + "'><div class='message'>" + message + "</div></div>").animate({
        opacity: 1,
        top: 0
      }, speed).appendTo($toastContainer);
      return setTimeout((function() {
        return $toast.animate({
          opacity: 0,
          top: "-" + distance
        }, speed, function() {
          return $toast.remove();
        });
      }), 4000);
    };

    return Toast;

  })();

  S.toast = function(message) {
    return new Toast(message);
  };

  showDialog = function() {
    var $dialog, $overlay;
    $overlay = $('<div>').appendTo($('body'));
    $overlay.css({
      position: 'fixed',
      width: "100%",
      height: "100%",
      top: 0,
      left: 0,
      zIndex: 10,
      background: "rgba(0,0,0,.5)"
    });
    $dialog = $('<div>').appendTo($overlay);
    $dialog.css({
      position: 'absolute',
      width: "60%",
      height: "60%",
      left: '20%',
      top: "20%",
      background: "#fff"
    });
    return {
      $overlay: $overlay,
      $dialog: $dialog
    };
  };

  S.toSignUpDialog = function(callback) {
    var $account, $dialog, $overlay, account, ref;
    ref = showDialog(), $overlay = ref.$overlay, $dialog = ref.$dialog;
    $dialog.append(ST.SignUp());
    $account = $('.account', $dialog);
    account = null;
    $('.nickname', $dialog).on('blur', function() {
      var nickname, q;
      nickname = $.trim($(this).val());
      if (!nickname) {
        return;
      }
      q = S.api.get('api/user/exists?nickname=' + nickname);
      q["catch"](S.alertAjaxError);
      return q.then(function(r) {
        if (r.exists) {
          return alert('昵称已存在');
        }
      });
    });
    $('.account', $dialog).on('blur', function() {
      var field, q;
      account = $.trim($(this).val());
      if (!account) {
        return;
      }
      field = account.indexOf('@') > 0 && 'email' || 'phone';
      q = S.api.get('api/user/exists?' + field + '=' + account);
      q["catch"](S.alertAjaxError);
      return q.then(function(r) {
        if (r.exists) {
          return alert(account + '已被注册');
        }
      });
    });
    $('.refresh-captcha', $dialog).click(function() {
      var img;
      img = $(this).children('img:first');
      return img.attr('src', '/captcha?_=' + (new Date().getTime()));
    });
    $('.send-code').click(function() {
      var captchaText, q, req;
      account = $.trim($account.val());
      if (!account) {
        return;
      }
      captchaText = $.trim($('.captcha', $dialog).val());
      if (!captchaText) {
        return;
      }
      req = {
        captchaText: captchaText
      };
      q = account.indexOf("@") > 0 ? S.api.post('api/security-code/email/' + account, req) : S.api.post('api/security-code/phone/' + account, req);
      q["catch"](S.alertAjaxError);
      return q.then(function() {
        return alert('发送成功');
      });
    });
    return $('.do-sign-up', $dialog).click(function() {
      var account2, code, password, q, req;
      account2 = $.trim($account.val());
      if (!(account2 && account && account2 === account)) {
        return;
      }
      code = $.trim($('.code', $dialog).val());
      password = $.trim($('.password', $dialog).val());
      req = account.indexOf("@") > 0 ? {
        email: account
      } : {
        phone: account
      };
      req.password = password;
      req.securityCode = code;
      req.nickname = $.trim($('.nickname', $dialog).val());
      q = S.api.post('api/sign-up', req);
      q["catch"](S.alertAjaxError);
      return q.then(function() {
        $overlay.remove();
        alert('成功');
        return S.toSignInDialog();
      });
    });
  };

  S.toSignInDialog = function(callback) {
    var $dialog, $overlay, ref;
    ref = showDialog(), $overlay = ref.$overlay, $dialog = ref.$dialog;
    $dialog.append(ST.SignIn());
    return $('.do-sign-in', $dialog).click(function() {
      var password, q, username;
      username = $('.username', $dialog).val();
      password = $('.password', $dialog).val();
      q = S.api.post("api/sign-in", {
        username: username,
        password: password
      });
      q["catch"](S.alertAjaxError);
      return q.then(function(userId) {
        $overlay.remove();
        if (callback) {
          return callback();
        } else {
          return location.reload();
        }
      });
    });
  };

  $(function() {
    S.toast('Error');
    $('.to-sign-in-dialog').click(function() {
      return S.toSignInDialog(function() {
        return location.reload();
      });
    });
    return $('.to-sign-up-dialog').click(function() {
      return S.toSignUpDialog();
    });
  });

}).call(this);
